package com.unitedvision.persistence.domain;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.unitedvision.core.domain.Operator;
import com.unitedvision.core.domain.Role;
import com.unitedvision.core.exception.WrongFormatException;

@Entity
@Table (name = "operator")
public class OperatorEntity {
	private int id;
	private CompanyEntity company;
	private String name;
	private String username;
	private String password;
	private Role role;
	private List<PaymentEntity> payments;
	private List<TransactionEntity> transactions;
	
	public OperatorEntity() {
		super();
	}

	public OperatorEntity(CompanyEntity companyEntity, String name, String username, String password, Role role) {
		super();
		this.company = companyEntity;
		this.name = name;
		this.username = username;
		this.password = password;
		this.role = role;
	}

	public OperatorEntity(Operator operator) throws WrongFormatException {
		super();
		this.company = CompanyEntity.create(operator.getCompany());
		this.name = operator.getName();
		this.username = operator.getUsername();
		this.password = operator.getPassword();
		this.role = operator.getRole();
	}

	@Id
	@GeneratedValue
	@Column (name = "id")
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@ManyToOne (fetch = FetchType.EAGER)
	@JoinColumn (name = "company_id", referencedColumnName = "id")
	public CompanyEntity getCompany() {
		return company;
	}

	public void setCompany(CompanyEntity companyEntity) {
		this.company = companyEntity;
	}

	@Column (name = "name")
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column (name = "username")
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	@Column (name = "password")
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Column (name = "role")
	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	@OneToMany (targetEntity = PaymentEntity.class, mappedBy = "operator", fetch = FetchType.LAZY)
	public List<PaymentEntity> getPayments() {
		return payments;
	}

	public void setPayments(List<PaymentEntity> paymentEntities) {
		this.payments = paymentEntities;
	}
	
	@OneToMany (targetEntity = TransactionEntity.class, mappedBy = "operator", fetch = FetchType.LAZY)
	public List<TransactionEntity> getTransactions() {
		return transactions;
	}

	public void setTransactions(List<TransactionEntity> transactions) {
		this.transactions = transactions;
	}

	@Transient
	public boolean isAuthenticated(String password) {
		return toOperator().isAuthenticated(password);
	}
	
	public Operator toOperator() {
		return new Operator(id, company.toCompany(), role, name, username, password);
	}
	
	public static OperatorEntity create(CompanyEntity companyEntity, String name, String username, String password, Role role) {
		return new OperatorEntity(companyEntity, name, username, password, role);
	}
	
	public static OperatorEntity create(Operator operator) throws WrongFormatException {
		return new OperatorEntity(operator);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((company == null) ? 0 : company.hashCode());
		result = prime * result + id;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result
				+ ((password == null) ? 0 : password.hashCode());
		result = prime * result + ((role == null) ? 0 : role.hashCode());
		result = prime * result
				+ ((username == null) ? 0 : username.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		
		OperatorEntity other = (OperatorEntity) obj;
		
		if (company == null) {
			if (other.company != null)
				return false;
		} else if (!company.equals(other.company))
			return false;
		
		if (id != other.id)
			return false;
		
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		
		if (password == null) {
			if (other.password != null)
				return false;
		} else if (!password.equals(other.password))
			return false;
		
		if (role != other.role)
			return false;
		
		if (username == null) {
			if (other.username != null)
				return false;
		} else if (!username.equals(other.username))
			return false;
		
		return true;
	}

	@Override
	public String toString() {
		return "OperatorEntity [id=" + id + ", company=" + company + ", name="
				+ name + ", username=" + username + ", password=" + password
				+ ", role=" + role + "]";
	}
}
